package com.heima.interceptor;

import cn.hutool.core.util.StrUtil;
import com.heima.utils.WMThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 * @author chenshan
 */
public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 路径的排除(排除登录)
        if (StrUtil.containsAny(request.getRequestURI(), "login/in")) {
            // 不需要做登录校验，直接放行
            return true;
        }

        // 需要进行登录的请求
        String id = request.getHeader("id");
        WMThreadLocal.set(Integer.parseInt(id));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        WMThreadLocal.remove();
    }
}
